package cn.iocoder.yudao.module.trade.controller.admin.giftcard;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.module.trade.controller.admin.giftcard.vo.*;
import cn.iocoder.yudao.module.trade.dal.dataobject.giftcard.GiftCardDO;
import cn.iocoder.yudao.module.trade.service.giftcard.GiftCardService;

@Tag(name = "管理后台 - 礼品卡")
@RestController
@RequestMapping("/gift/card")
@Validated
public class CardController {

    @Resource
    private GiftCardService cardService;

    @PostMapping("/create")
    @Operation(summary = "创建礼品卡")
    public CommonResult<Long> createCard(@Valid @RequestBody CardSaveReqVO createReqVO) {
        return success(cardService.createCard(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新礼品卡")
    public CommonResult<Boolean> updateCard(@Valid @RequestBody CardSaveReqVO updateReqVO) {
        cardService.updateCard(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除礼品卡")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteCard(@RequestParam("id") Long id) {
        cardService.deleteCard(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得礼品卡")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<CardRespVO> getCard(@RequestParam("id") Long id) {
        GiftCardDO card = cardService.getCard(id);
        return success(BeanUtils.toBean(card, CardRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得礼品卡分页")
    public CommonResult<PageResult<CardRespVO>> getCardPage(@Valid CardPageReqVO pageReqVO) {
        PageResult<GiftCardDO> pageResult = cardService.getCardPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CardRespVO.class));
    }

}